package homes;

import static org.junit.Assert.assertTrue;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;

import ExcepcionesDelJuego.JugadaExc;
import ExcepcionesDelJuego.UserOPasswordInvalidoException;
import ExcepcionesDelJuego.UsuarioExisteteException;
import ExcepcionesDelJuego.UsuarioYaLogueado;

import juego.Usuario;

public class JuegoHome {

	public void registrarUsuario(String usr, String pass) {
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
			conn = new ConectorHome().getConnection();
			stmt = conn.prepareStatement("SELECT * FROM usuarios WHERE usuario = ?");
			stmt.setString(1, usr);
			ResultSet rs = stmt.executeQuery();
			if (!rs.next()) {
				stmt = conn.prepareStatement("INSERT INTO usuarios(usuario,password,fechaAlta) VALUES(?,?,?)");
				stmt.setString(1, usr);
				stmt.setString(2, pass);
				stmt.setDate(3, new Date(Calendar.getInstance().getTimeInMillis()));
				int rs2 = stmt.executeUpdate();
			} else {
				throw new UsuarioExisteteException();
			}

		} catch (SQLException e) {
			throw new JugadaExc(e);
		} finally {
			closeConection(conn, stmt);
		}

	}

	private void closeConection(Connection conn, PreparedStatement stmt) {
		if (conn != null)
			try {
				conn.close();
				stmt.close();
			} catch (SQLException e) {
				throw new JugadaExc(e);
			}
	}

	public Usuario loguear(String usr, String pass) {
		Usuario user = new Usuario();
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
			conn = new ConectorHome().getConnection();
			stmt = conn
					.prepareStatement("SELECT * FROM usuarios WHERE usuario = ? AND password = ? ");
			stmt.setString(1, usr);
			stmt.setString(2, pass);
			ResultSet rs = stmt.executeQuery();
			if (rs.next()) {
				if (!rs.getBoolean("loguedIn")){

				user.setUsuario(rs.getString("usuario"));
				user.setPass(rs.getString("password"));
				user.setNombre(rs.getString("nombre"));
				user.setMail(rs.getString("mail"));
				user.setApellido(rs.getString("apellido"));
				user.setAlta(rs.getDate("fechaAlta"));
				stmt = conn.prepareStatement("UPDATE usuarios SET loguedIn = true WHERE usuario = ? AND password = ?");
				stmt.setString(1, usr);
				stmt.setString(2, pass);
				int  rs2 = stmt.executeUpdate();
				}else{
					throw new UsuarioYaLogueado();
				}

			}else{
				throw new UserOPasswordInvalidoException();
			}
		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			closeConection(conn, stmt);
		}

		return user;

	}

	public void cambiarPasswoord(String user, String passActual,
			String nuevaPass) {
		Connection conn = null;
		PreparedStatement stmt = null;

		try {
			conn = new ConectorHome().getConnection();
			stmt = conn
					.prepareStatement("SELECT * FROM usuarios WHERE usuario = ? AND password = ? ");
			stmt.setString(1, user);
			stmt.setString(2, passActual);
			ResultSet res1 = stmt.executeQuery();
			if (res1.next()) {

				stmt = conn
						.prepareStatement("UPDATE usuarios SET password = ?, fechaDeModificacion = ? WHERE usuario = ? AND password = ?");
				stmt.setString(1, nuevaPass);
				stmt.setDate(2, new Date(Calendar.getInstance().getTimeInMillis()));
				stmt.setString(3, user);
				stmt.setString(4, passActual);
				int rs2 = stmt.executeUpdate();
			}else{
				throw new UserOPasswordInvalidoException();
			}
		} catch (SQLException e) {

			throw new JugadaExc(e);
		} finally {
			closeConection(conn, stmt);
		}

	}

	public boolean existeUsuario(String usr) {
		Connection conn = null;
		PreparedStatement stmt = null;
		boolean existe = false;

		try {
			conn = new ConectorHome().getConnection();
			stmt = conn.prepareStatement("SELECT usuario FROM usuarios WHERE usuario = ?");
			stmt.setString(1, usr);
			ResultSet res1 = stmt.executeQuery();
			if (res1.next()) {
				existe = true;
			}

		} catch (SQLException e) {

			throw new JugadaExc(e);

		} finally {
			closeConection(conn, stmt);

		}
		return existe;
		
	}
	
	public boolean darDeBajaUsuario(String user, String pass){
		Connection conn = null;
		PreparedStatement stmt = null;
		boolean borro = false;
		
		try {
			conn = new ConectorHome().getConnection();
			stmt = conn.prepareStatement("DELETE FROM usuarios WHERE usuario = ? AND password = ?");
			stmt.setString(1, user);
			stmt.setString(2, pass);
			int res1 = stmt.executeUpdate();
			if(res1==1 && !existeUsuario(user)){
				borro = true;
			}

		} catch (SQLException e) {

			throw new JugadaExc(e);

		} finally {
			closeConection(conn, stmt);			
		}
		return borro;
	}
	
	
	public Usuario logOut(String usr) {
		Usuario user = new Usuario();
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
			conn = new ConectorHome().getConnection();
			stmt = conn
					.prepareStatement("SELECT * FROM usuarios WHERE usuario = ?");
			stmt.setString(1, usr);
			
			ResultSet rs = stmt.executeQuery();
			
				

				stmt = conn.prepareStatement("UPDATE usuarios SET loguedIn = false WHERE usuario = ? ");
				stmt.setString(1, usr);
			
				int  rs2 = stmt.executeUpdate();
				

			
		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			closeConection(conn, stmt);
		}

		return user;

	}
	
}
